﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.AspNetCore.Repository/EfRepository 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       EfRepository
* 创建时间：  2025/5/22 14:52:59
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion 
using Dapper;
using Microsoft.EntityFrameworkCore;
using SSPivot.AspNetCore.Config;
using SSPivot.Common;
using SSPivot.Common.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.AspNetCore.Repository
{
    public abstract class EfRepository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        #region 属性注入DbContext 
        private readonly IDbContextFactory _dbContextFactory;
        protected EfRepository(IDbContextFactory dbContextFactory)
        {
                _dbContextFactory = dbContextFactory;
        }
        /// <summary>
        /// db上下文
        /// </summary>
        public DbContext DbContext { get; set; }
        public DbContext ReadDbContext { get; set; }
        /// <summary>
        /// 获取上下文
        /// </summary>
        /// <returns></returns>
        public DbContext GetDbContext()
        {
            //var config = ConfigHelper.GetOptions<DbConfig>("DbConfig");
            //return _dbContextFactory.CreateDbContext() ;
            return this.DbContext;
        }
        public DbContext GetReadDbContext()
        {
            //var config = ConfigHelper.GetOptions<DbConfig>("DbConfig");
            //return _dbContextFactory.CreateReadDbContext(); 
            return this.ReadDbContext;
        }

        #endregion


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity">待新增实体</param>
        /// <returns>新增后实体</returns>
        public virtual async ValueTask<TEntity> AddNotCommitAsync(TEntity entity)
        {
            var result = await GetDbContext().Set<TEntity>().AddAsync(entity);
            return result.Entity;
        }

        /// <summary>
        /// 插入(立即提交到数据库)
        /// </summary>
        /// <param name="entity">待插入实体</param>
        /// <returns>插入后实体</returns>
        public virtual async ValueTask<TEntity> AddAsync(TEntity entity)
        {

            var result = await GetDbContext().Set<TEntity>().AddAsync(entity);
            await GetDbContext().SaveChangesAsync();
            return result.Entity;
        }

        /// <summary>
		/// 批量新增
		/// </summary>
		/// <param name="entity">待新增实体</param>
		/// <returns>新增后实体</returns>
		public virtual async Task AddRangeNotCommitAsync(IEnumerable<TEntity> entity)
        {
            await GetDbContext().Set<TEntity>().AddRangeAsync(entity);
        }
        /// <summary>
        /// 批量插入(立即提交到数据库)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual async Task AddRangeAsync(IEnumerable<TEntity> entity)
        {
            await GetDbContext().Set<TEntity>().AddRangeAsync(entity);
            await GetDbContext().SaveChangesAsync();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public virtual async Task DeleteNotCommitAsync<T>(T id)
        {
            var dbEneity = await DbContext.Set<TEntity>().FindAsync(id);
            DbContext.Set<TEntity>().Remove(dbEneity);
        }

        /// <summary>
        /// 删除(立即提交到数据库)
        /// </summary>
        /// <param name="id">id</param>
        public virtual async Task DeleteAsync<T>(T id)
        {
            var dbEneity = await DbContext.Set<TEntity>().FindAsync(id);
            DbContext.Set<TEntity>().Remove(dbEneity);
            await DbContext.SaveChangesAsync();
        }


        /// <summary>
        /// 删除(立即提交到数据库)
        /// </summary>
        /// <param name="entity">待删除的实体</param>
        public virtual async Task DeleteAsync(TEntity entity)
        {
            DbContext.Set<TEntity>().Remove(entity);
            await DbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 删除多个(立即提交到数据库)
        /// </summary>
        /// <param name="entities">待删除的实体集合</param>
        public virtual async Task DeleteRangeAsync(IEnumerable<TEntity> entities)
        {
            DbContext.Set<TEntity>().RemoveRange(entities);
            await DbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 更新(立即提交到数据库)
        /// </summary>
        /// <param name="entity">待更新实体</param>
        /// <returns>更新实体</returns>
        public virtual async ValueTask<TEntity> UpdateAsync(TEntity entity)
        {
            DbContext.Entry<TEntity>(entity).State = EntityState.Modified;
            await DbContext.SaveChangesAsync();
            return entity;
        }

        /// <summary>
        /// 查找单个实体 (根据主键)
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>查询到的结果</returns>
        public virtual async ValueTask<TEntity> GetAsync(object id)
        {
            return await ReadDbContext.Set<TEntity>().FindAsync(id);
        }

        /// <summary>
        /// 查找单个实体 (根据条件)
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns>查询到的结果</returns>
        public virtual async ValueTask<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await ReadDbContext.Set<TEntity>().Where(predicate).FirstOrDefaultAsync();
        }

        #region noTracking
        /// <summary>
        /// 查询所有实体
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query()
        {
            return ReadDbContext.Set<TEntity>().AsQueryable();
        }

        /// <summary>
        /// 查询所有实体(有条件)
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> predicate)
        {
            return ReadDbContext.Set<TEntity>().Where(predicate);
        }

        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return Query(orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query(string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().AsQueryable();
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序（多个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().AsQueryable();
            //排序
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return Query<TKey>(predicate, orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
		public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().Where(predicate);
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }


        /// <summary>
        /// 查询所有实体并排序 (有条件)（多个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query<TKey>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().Where(predicate);
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return Query<TKey>(orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query(string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().AsQueryable();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (多个排序字段)(分页)
        /// </summary>
        /// <param name="page">分页信息</param>
        /// <param name="orderBy">排序</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> Query<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().AsQueryable();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return Query<TKey>(predicate, orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }


        /// <summary>
        /// 查询所有实体并排序 (有条件)(多个排序字段)(分页)
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="page">分页信息</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> Query<TValue>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TValue>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }


        /// <summary>
        /// 查询所有实体
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking()
        {
            return ReadDbContext.Set<TEntity>().AsNoTracking().AsQueryable();
        }

        /// <summary>
		/// 查询所有实体(有条件)
		/// </summary>
		/// <param name="predicate">条件</param>
		/// <returns>查询到的结果</returns>
		public virtual IQueryable<TEntity> QueryNoTracking(Expression<Func<TEntity, bool>> predicate)
        {
            return ReadDbContext.Set<TEntity>().AsNoTracking().Where(predicate);
        }


        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTracking(orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking(string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().AsNoTracking().AsQueryable();
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }

        /// <summary>
		/// 查询所有实体并排序（多个排序字段）
		/// </summary>
		/// <typeparam name="TKey"></typeparam>
		/// <param name="orderBy"></param>
		/// <returns></returns>
		public virtual IQueryable<TEntity> QueryNoTracking<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().AsNoTracking().AsQueryable();
            //排序
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTracking<TKey>(predicate, orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
		public virtual IQueryable<TEntity> QueryNoTracking(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().AsNoTracking().Where(predicate);
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（多个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
		public virtual IQueryable<TEntity> QueryNoTracking<TKey>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().AsNoTracking().Where(predicate);
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTracking<TKey>(orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking(string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().AsNoTracking();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (多个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().AsNoTracking().AsQueryable();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTracking<TKey>(predicate, orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTracking(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null) page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().AsNoTracking().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(多个排序字段)(分页)
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="page">分页信息</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> QueryNoTracking<TValue>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TValue>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().AsNoTracking().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }
        #endregion

        #region ignorequeryfilter

        /// <summary>
        /// 查询所有实体
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters()
        {
            return ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsQueryable();
        }

        /// <summary>
        /// 查询所有实体(有条件)
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters(Expression<Func<TEntity, bool>> predicate)
        {
            return ReadDbContext.Set<TEntity>().IgnoreQueryFilters().Where(predicate);
        }

        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryIgnoreQueryFilters(orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters(string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsQueryable();
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序（多个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsQueryable();
            //排序
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryIgnoreQueryFilters<TKey>(predicate, orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
		public virtual IQueryable<TEntity> QueryIgnoreQueryFilters(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().Where(predicate);
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }


        /// <summary>
        /// 查询所有实体并排序 (有条件)（多个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TKey>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().Where(predicate);
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryIgnoreQueryFilters<TKey>(orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters(string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsQueryable();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (多个排序字段)(分页)
        /// </summary>
        /// <param name="page">分页信息</param>
        /// <param name="orderBy">排序</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsQueryable();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryIgnoreQueryFilters<TKey>(predicate, orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }


        /// <summary>
        /// 查询所有实体并排序 (有条件)(多个排序字段)(分页)
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="page">分页信息</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> QueryIgnoreQueryFilters<TValue>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TValue>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }


        /// <summary>
        /// 查询所有实体
        /// </summary>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters()
        {
            return ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().AsQueryable();
        }

        /// <summary>
		/// 查询所有实体(有条件)
		/// </summary>
		/// <param name="predicate">条件</param>
		/// <returns>查询到的结果</returns>
		public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters(Expression<Func<TEntity, bool>> predicate)
        {
            return ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().Where(predicate);
        }


        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTrackingIgnoreQueryFilters(orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序（单个排序字段）
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters(string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().AsQueryable();
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }

        /// <summary>
		/// 查询所有实体并排序（多个排序字段）
		/// </summary>
		/// <typeparam name="TKey"></typeparam>
		/// <param name="orderBy"></param>
		/// <returns></returns>
		public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().AsQueryable();
            //排序
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTrackingIgnoreQueryFilters<TKey>(predicate, orderByP);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（单个排序字段）
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <returns></returns>
		public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().Where(predicate);
            //排序
            query = query.SortBy(orderBy, sortType);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)（多个排序字段）
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
		public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TKey>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy)
        {
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().Where(predicate);
            query = query.SortBy(orderBy);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TKey>(Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTrackingIgnoreQueryFilters<TKey>(orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (单个排序字段)(分页)
        /// </summary>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters(string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (多个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="orderBy"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TKey>(Dictionary<Expression<Func<TEntity, TKey>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().AsQueryable();
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TKey>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TKey>> orderBy, SortType sortType, IPageResult page)
        {
            var orderByP = new Dictionary<Expression<Func<TEntity, TKey>>, SortType>() { { orderBy, sortType } };
            return QueryNoTrackingIgnoreQueryFilters<TKey>(predicate, orderByP, page);
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(单个排序字段)(分页)
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="orderBy"></param>
        /// <param name="sortType"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters(Expression<Func<TEntity, bool>> predicate, string orderBy, SortType sortType, IPageResult page)
        {
            if (page == null) page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy, sortType);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        /// <summary>
        /// 查询所有实体并排序 (有条件)(多个排序字段)(分页)
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="orderBy">排序</param>
        /// <param name="page">分页信息</param>
        /// <returns>查询到的结果</returns>
        public virtual IQueryable<TEntity> QueryNoTrackingIgnoreQueryFilters<TValue>(Expression<Func<TEntity, bool>> predicate, Dictionary<Expression<Func<TEntity, TValue>>, SortType> orderBy, IPageResult page)
        {
            if (page == null)
                page = PageResult.Default();
            var query = ReadDbContext.Set<TEntity>().IgnoreQueryFilters().AsNoTracking().Where(predicate);
            page.RecordCount = query.Count();
            //排序
            query = query.SortBy(orderBy);
            //获取分页数据
            int skip = (page.PageNo - 1) * page.PageSize;
            query = query.Skip(skip).Take(page.PageSize);
            return query;
        }

        #endregion

        public virtual IQueryable<T> SelectQuery<T>(string strSql, string[] parameters)
        {
            return this.GetReadDbContext().Database.GetDbConnection().Query<T>(strSql, parameters).AsQueryable();
        }
    }
}
